import router from './router'
import store from './store'
import {
  Message
} from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {
  getToken
} from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({
  showSpinner: false
}) // NProgress Configuration

const whiteList = ['/web/index'] // no redirect whitelist


router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start()
  // set page title
  document.title = getPageTitle(to.meta.title)
  // next();
  //determine whether the user has logged in
  const hasToken = getToken()
  if (hasToken && sessionStorage.getItem("token")) {
    if (to.path === '/web/index') {
      // if is logged in, redirect to the home page
      next({
        path: '/'
      })
      NProgress.done()
    } else {
      const hasGetUserInfo = store.getters.name
      const is_admin_view = store.getters.is_admin_view
      if (hasGetUserInfo) {
        // console.log(is_admin_view)
        next()
      } else {
        try {
          // get user info
          // console.log(store,'state');
          const {
            is_admin
          } = await store.dispatch('user/getInfo'); //拿到当前的用户信息 角色
          // generate accessible routes map based on roles
          await store.dispatch('user/changeRoles', {
            role: is_admin
          });
          next({
            ...to,
            replace: true
          })
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error({
            message: error,
            type: 'error',
          })
          // next(`/web/home?redirect=${to.path}`)
          next(`/web/index`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      // next(`/web/home?redirect=${to.path}`)
      // next(`/web/home`)
      if (to.path == '/detail' || to.path == '/power' || to.path == '/instructions' || to.path == '/drawings' || to.path == '/fullText' ||
        to.path == '/law' || to.path == '/kindred' || to.path == "/web/news" || to.path == "/web/activities" || to.path == "/web/engineering" || to.path == "/web/protection" ||
        to.path == "/web/manufacturing" || to.path == "/web/honor" || to.path == "/web/facturing/details" || to.path == "/web/engineering/details" || to.path == "/web/protection/details" ||
        to.path == "/web/news/details" || to.path == "/web/activities/details" || to.path == "/web/services/central" || to.path == "/web/services/dynamic" ||
        to.path == "/web/services/introduce" || to.path == "/web/central/details" || to.path == "/web/finance" || to.path == "/web/enterprise" ||
        to.path == "/web/enterprise/leader" || to.path == '/web/leader/details' || to.path == "/web/overseas/warning" || to.path == "/web/home" || to.path == "/web/askAbout" || to.path == "/web/seniorSearch" ||
        to.path == "/web/manage" || to.path == '/web/guanjia' || to.path == '/web/overseas/assistance' || to.path == '/web/overseas/association' ||
        to.path == "/web/search" || to.path == "/web/search/list" || to.path == "/assistance/details" || to.path == "/association/details" || to.path == "/web/policies" ||
        to.path == "/web/dialogue" || to.path == "/web/patentaward" || to.path == "/web/policies/details" || to.path == "/web/policy/details" || to.path == "/web/immediately" ||
        to.path == "/web/services/InnovationStory" || to.path == "/web/manageNew" || to.path == "/web/small/login" ||
        to.path == "/web/small/Waitvisit" || to.path == "/web/small/workVisit" || to.path == "/web/addWaitvisit"
      ) {
        next()
      } else {
        if (to.path == "/newhome" || to.path == "/visualization" || to.path == "/visualization/Visit" || to.path == "/visualization/Patent" || to.path == "/work" || to.path == "/workCompany" || to.path == "/workWarning" ||
          to.path == "/workTobear" || to.path == "/workFollow" || to.path == "/workVisit" || to.path == "/workVisit/details" || to.path == "/workCalendar" || to.path == "/workWaitvisit" || to.path == "/addWaitvisit" ||
          to.path == "/workEnterprisevisit" || to.path == "/datapage" || to.path == "/honor" || to.path == "/newsystem" || to.path == "/newUserCenter"|| to.path == "/NewupPwd"|| to.path == "/companyDetails") {
          let isNewHome = localStorage.getItem("isNewHome");
          if (isNewHome) {
            next(`/web/index`)
          } else {
            next(`/web/index`)
            // next(`/web/manageNew`)
          }
          localStorage.removeItem("isNewHome");
        } else {
          next(`/web/index`)
        }
      }
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
